package com.zdy.im.user.facade;

import com.sixeco.framework.common.enums.TbStatusEnum;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.http.exception.SystemRuntimeException;
import com.zdy.im.enums.ImResultCodeEnum;
import com.zdy.im.user.dto.response.ImUserFriendShipResponseDto;
import com.zdy.im.user.service.ImUserFriendShipService;
import com.zdy.im.user.vo.request.UserGroupInfoRequestVo;
import com.zdy.im.user.vo.response.ImUserGroupInfoShowResponseVo;
import com.zdy.im.user.dto.request.ImUserGroupInfoRequestDto;
import com.zdy.im.user.dto.response.ImUserGroupInfoResponseDto;
import com.zdy.im.user.service.ImUserGroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**   
 * @Title: ImUserGroupInfoFacade.java
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0  
 *  <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 * @Description: TODO(说明) 对象缓存统一模板实现类;  
 */
 
@Component
public class ImUserGroupInfoFacade {

	@Autowired
	private ImUserGroupInfoService imUserGroupInfoService;
	@Autowired
	private ImUserFriendShipService imUserFriendShipService;

	/**
	 * 新增组
	 * @param friendVo
	 * @return
	 */
	public boolean save( UserGroupInfoRequestVo friendVo){
		//校验该组是否存在
		ImUserGroupInfoResponseDto imUserGroupInfoNameDto = imUserGroupInfoService.selectGroupName(friendVo.getUserId(),friendVo.getGroupName());
		if((friendVo.getId() == 0 && imUserGroupInfoNameDto != null)
				|| (friendVo.getId() > 0 && imUserGroupInfoNameDto != null && imUserGroupInfoNameDto.getId() != friendVo.getId())){
			throw new SystemRuntimeException(ImResultCodeEnum.IM_USER_GROUP_YES_EXIST);
		}
		//修改组
		if(friendVo.getId() > 0){
			//查询组
			ImUserGroupInfoResponseDto imUserGroupInfoDto = imUserGroupInfoService.getImUserGroupInfoById(friendVo.getId());
			if(imUserGroupInfoDto == null || (imUserGroupInfoDto !=null && imUserGroupInfoDto.getUserId() != friendVo.getUserId())){
				throw new SystemRuntimeException(ImResultCodeEnum.IM_USER_GROUP_NOT_EXIST);
			}
			ImUserGroupInfoRequestDto imUserGroupInfoRequestDto = ImUserGroupInfoRequestDto.build().clone(imUserGroupInfoDto);
			imUserGroupInfoRequestDto.toGroupName(friendVo.getGroupName());
			return imUserGroupInfoService.updateImUserGroupInfo(imUserGroupInfoRequestDto);
		}
		//查询某用户，组的最大序号
		int sort = 0 ;
		ImUserGroupInfoResponseDto userGroupInfo = imUserGroupInfoService.getOneByUserId(friendVo.getUserId());
		if(userGroupInfo != null){
			sort = userGroupInfo.getSort() + 1;
		}
		//新增用户组
		ImUserGroupInfoRequestDto imUserGroupInfoRequestDto = ImUserGroupInfoRequestDto.build()
				.toUserId(friendVo.getUserId())
				.toGroupName(friendVo.getGroupName())
				.toSort(sort)
				.toStatus(TbStatusEnum.ENABLE.index());
		ImUserGroupInfoResponseDto imUserGroupInfoResponseDto = imUserGroupInfoService.saveImUserGroupInfo(imUserGroupInfoRequestDto);
		if(imUserGroupInfoResponseDto == null){
			return false;
		}
		return true;
	}

	/**
	 * 删除组
	 * @param groupId
	 * @return
	 */
	public int del( long groupId){
		ImUserGroupInfoResponseDto imUserGroupInfoDto = imUserGroupInfoService.getImUserGroupInfoById(groupId);
		if(imUserGroupInfoDto == null){
			throw new SystemRuntimeException(ImResultCodeEnum.IM_USER_GROUP_NOT_EXIST);
		}
		List<ImUserFriendShipResponseDto> userFriendShipDtos = imUserFriendShipService.selectListByUserGroupId(groupId);
		if(userFriendShipDtos != null){
			throw new SystemRuntimeException(ImResultCodeEnum.IM_USER_GROUP_CODE);
		}

		List<Long> ids = new ArrayList<>();
		ids.add(groupId);
		return imUserGroupInfoService.delImUserGroupInfoByIds(ids);
	}

	/**
	 * 查询该用户下，所有组
	 * @param userId
	 * @return
	 */
	public ResponseResultList<ImUserGroupInfoShowResponseVo> selectListByUserId(long userId){
		List<ImUserGroupInfoResponseDto> resultList = imUserGroupInfoService.selectListByUserId(userId);
		if(null == resultList || resultList.isEmpty() ){
			return ResponseResultList.build();
		}
		List<ImUserGroupInfoShowResponseVo> listVo = new ArrayList<>();
		resultList.forEach(e -> listVo.add(ImUserGroupInfoShowResponseVo.build().clone(e)));
		return ResponseResultList.build().toList(listVo).toTotal(listVo.size());
	}

}
